import request from '@/utils/request'
import { API_CONFIG } from '@/api/config'

// 查询商品列表
export function listProduct(query) {
  const params = {
    ...query,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/fun/product/list',
    method: 'get',
    params: params
  })
}

// 查询商品详细
export function getProduct(productId) {
  return request({
    url: '/fun/product/' + productId,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 移动端商品详情
export function getMobileProductDetail(productId) {
  return request({
    url: '/mobile/product/' + productId,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 新增商品
export function addProduct(data) {
  const requestData = {
    ...data,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/fun/product',
    method: 'post',
    data: requestData
  })
}

// 修改商品
export function updateProduct(data) {
  const requestData = {
    ...data,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/fun/product',
    method: 'put',
    data: requestData
  })
}

// 删除商品
export function delProduct(productId) {
  return request({
    url: '/fun/product/' + productId,
    method: 'delete',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 获取热销商品
export function getHotProducts(query = {}) {
  return request({
    url: '/fun/product/list',
    method: 'get',
    params: {
      ...query,
      orgId: API_CONFIG.ORG_ID,
      isHot: '1',
      publishStatus: '1',
      status: '0'
    }
  })
}

// 获取推荐商品
export function getRecommendProducts(query = {}) {
  return request({
    url: '/fun/product/list',
    method: 'get',
    params: {
      ...query,
      orgId: API_CONFIG.ORG_ID,
      isRecommend: '1',
      publishStatus: '1',
      status: '0'
    }
  })
}

// 获取新品商品
export function getNewProducts(query = {}) {
  return request({
    url: '/fun/product/list',
    method: 'get',
    params: {
      ...query,
      orgId: API_CONFIG.ORG_ID,
      isNew: '1',
      publishStatus: '1',
      status: '0'
    }
  })
}

// 移动端商品接口
// 移动端商品列表
export function listMobileProduct(query) {
  const params = {
    ...query,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/product/list',
    method: 'get',
    params: params
  })
}

// 移动端热销商品列表
export function listMobileHotProduct(query) {
  const params = {
    ...query,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/product/hot',
    method: 'get',
    params: params
  })
}

// 移动端推荐商品列表
export function listMobileRecommendProduct(query) {
  const params = {
    ...query,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/product/recommend',
    method: 'get',
    params: params
  })
}

// 移动端新品商品列表
export function listMobileNewProduct(query) {
  const params = {
    ...query,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/product/new',
    method: 'get',
    params: params
  })
}

// 根据分类查询移动端商品列表
export function listMobileProductByCategory(query) {
  const params = {
    ...query,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/product/category',
    method: 'get',
    params: params
  })
}

// 更新商品销量
export function updateProductSaleCount(productId) {
  return request({
    url: '/fun/product/updateSaleCount/' + productId,
    method: 'post',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 更新SKU销量
export function updateSkuSaleCount(skuId, increment) {
  return request({
    url: '/fun/sku/updateSaleCount/' + skuId,
    method: 'post',
    data: {
      increment: increment,
      orgId: API_CONFIG.ORG_ID
    }
  })
}